package me.firstapp.service.bbs.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.BbsNotification;
import me.firstapp.module.bbs.Member;
import me.firstapp.module.bbs.Reply;
import me.firstapp.module.bbs.Topic;
import me.firstapp.repository.bbs.BbsNotificationRepository;
import me.firstapp.repository.bbs.MemberRepository;
import me.firstapp.repository.bbs.ReplyRepository;
import me.firstapp.repository.bbs.TopicRepository;
import me.firstapp.service.bbs.ReplyService;

@Service
public class ReplyServiceImpl implements ReplyService {

	@Autowired
	private ReplyRepository replyRepository;

	@Autowired
	private TopicRepository topicRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private BbsNotificationRepository bbsNotificationRepository;

	@Override
	public Long findTopicReplyCounts(Long topicId, Long apiClientId) {
		if (topicId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "话题id");
		}
		return replyRepository.findTopicReplyCounts(topicId, apiClientId);
	}

	@Override
	public Page<Reply> findRepliesPageByTopicId(Integer pageNo, Integer pageSize, Long topicId, Long fromMemberId,
			Long toMemberId, Long apiClientId) {
		return replyRepository.findRepliesPage(pageNo, pageSize, topicId, fromMemberId, toMemberId, apiClientId);
	}

	@Override
	public Reply addReply(String content, Long topicId, Long fromMemberId, Long toMemberId, Long apiClientId) {
		if (topicId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "帖子id");
		}
		if (content == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "回复内容");
		}
		if (fromMemberId == null || toMemberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		Topic topic = (Topic) topicRepository.findTopicById(topicId, apiClientId);
		if (topic == null) {
			throw new ServiceException(StatusHouse.TOPIC_NOT_EXIST);
		}
		Member fromMember = (Member) memberRepository.findMemberById(fromMemberId, apiClientId);
		Member toMember = (Member) memberRepository.findMemberById(toMemberId, apiClientId);
		if (fromMember == null || toMember == null) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_FOUND);
		}
		if (fromMember.getStatus() == 0) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_USE);
		}
		Reply reply = new Reply();
		reply.setContent(content);
		reply.setCreateTime(new Date());
		reply.setTopicId(topicId);
		reply.setFromMember(fromMember);
		reply.setToMember(toMember);
		reply.setApiClientId(apiClientId);
		replyRepository.save(reply);

		// 写入通知，不进行事务
		try {
			if (!fromMemberId.equals(toMemberId)) {// 自己不能通知自己
				BbsNotification bbsNotification = new BbsNotification();
				if (toMemberId == topic.getMember().getId()) {// 评论帖子
					bbsNotification.setAction(1);
				} else {// 回复他人
					bbsNotification.setAction(2);
				}
				bbsNotification.setContent(content);
				bbsNotification.setFromMember(fromMember);
				bbsNotification.setIsRead(0);
				bbsNotification.setNotifyTime(new Date());
				bbsNotification.setToMember(toMember);
				bbsNotification.setTopic(topic);
				bbsNotification.setApiClientId(apiClientId);
				bbsNotificationRepository.save(bbsNotification);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return reply;
	}

	@Override
	public Page<Reply> findRepliesPageByParams(Long topicId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Long apiClientId) {
		return replyRepository.findRepliesPageByParams(topicId, pageNo, pageSize, searchParams, sortParam, sortType,
				apiClientId);
	}

	@Override
	public void deleteReplies(String ids, Long apiClientId) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "回复id");
		}
		if (!ids.contains(",")) {
			Reply reply = replyRepository.findReplyById(Long.valueOf(ids), apiClientId);
			if (reply != null) {
				replyRepository.deleteById(Long.valueOf(ids));
			}
		} else {
			String[] _ids = ids.split(",");
			Long[] _ids_ = new Long[_ids.length];
			for (int i = 0; i < _ids.length; i++) {
				Reply reply = replyRepository.findReplyById(Long.valueOf(_ids[i]), apiClientId);
				if (reply != null) {
					_ids_[i] = Long.valueOf(_ids[i]);
				}
			}
			if (_ids.length > 0) {
				replyRepository.delete(_ids_);
			}
		}
	}

	@Override
	public Reply findReplyById(Long id, Long apiClientId) {
		return replyRepository.findReplyById(id, apiClientId);
	}

}
